package com.sun.showcase.controller.mall;

import com.aliyuncs.utils.StringUtils;
import com.sun.showcase.biz.basic.service.BasicFeignService;
import com.sun.showcase.biz.mall.service.ShopCashApplyService;
import com.sun.showcase.client.domain.basic.UserInfo;
import com.sun.showcase.client.domain.mall.ShopCashApply;
import com.sun.showcase.client.query.mall.ShopCashApplyQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.LoginContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value="/mall/shopCashApply")
public class ShopCashApplyController{
	
	@Autowired
	private ShopCashApplyService shopCashApplyService;
	@Autowired
	private BasicFeignService basicFeignService;
	
	private Result result = new Result();
	/**
	 * 跳转到ShopCashApply管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goShopCashApply")
	public String goShopCashApply() {
		return "mall/shopCashApply";
	}
	/**
	 * 跳转到ShopCashApply管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goApply")
	public String goApply(ModelMap map) {
		UserInfo ui = basicFeignService.get(LoginContextHolder.get().getUserId());
		if(ui == null || ui.getBalance() == null) {
			map.put("balance", 0);
		}else {
			map.put("balance", ui.getBalance());
		}
		return "mall/shopCashApply_shop";
	}
	/**
	 * 跳转到ShopCashApply新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleShopCashApply")
	public String handleShopCashApply(@RequestParam(value="id",required=false) Long id , ModelMap map) {
		return "mall/shopCashApply_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public ShopCashApply showDesc(ShopCashApplyQuery shopCashApplyQuery) {
		return shopCashApplyService.get(shopCashApplyQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(ShopCashApplyQuery shopCashApplyQuery) {
		return shopCashApplyService.datagrid(shopCashApplyQuery);
	}
	
	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagridShop")
	public DataGrid datagridShop(ShopCashApplyQuery shopCashApplyQuery) {
		shopCashApplyQuery.setUserId(LoginContextHolder.get().getUserId());
		return shopCashApplyService.datagrid(shopCashApplyQuery);
	}
	/**
	 * 添加一个ShopCashApply
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(ShopCashApplyQuery shopCashApplyQuery) {
		if(StringUtils.isEmpty(LoginContextHolder.get().getShopId())) {
			result.setSuccess(false);
			result.setMsg("您无权发起申请！");
			return result;
		}
		UserInfo ui = basicFeignService.get(LoginContextHolder.get().getUserId());
		if(ui == null || ui.getBalance() == null) {
			result.setSuccess(false);
			result.setMsg("您的余额不足！");
			return result;
		}else if(ui.getBalance()<shopCashApplyQuery.getAmount()){
			result.setSuccess(false);
			result.setMsg("您的余额不足！");
			return result;
		}
		shopCashApplyQuery.setShopId(LoginContextHolder.get().getShopId());
		shopCashApplyQuery.setShopName(LoginContextHolder.get().getShopName());
		shopCashApplyQuery.setUserId(LoginContextHolder.get().getUserId());
		shopCashApplyQuery.setUserName(LoginContextHolder.get().getUserNickName());
		
		ShopCashApply t = shopCashApplyService.add(shopCashApplyQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		result.setData(t.getAmount());
		return result;
	}

	/**
	 * 编辑ShopCashApply
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(ShopCashApplyQuery shopCashApplyQuery) {
		shopCashApplyService.update(shopCashApplyQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}

	/**
	 * 物理删除ShopCashApply
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(ShopCashApplyQuery shopCashApplyQuery) {
		shopCashApplyService.delete(shopCashApplyQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	
	/**
	 * 逻辑删除ShopCashApply
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Result deletePt(ShopCashApplyQuery shopCashApplyQuery) {
		shopCashApplyService.deletePt(shopCashApplyQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	/**
	 * 全部数据ShopCashApply
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(ShopCashApplyQuery shopCashApplyQuery) {
		List<ShopCashApplyQuery> list = shopCashApplyService.listAll(shopCashApplyQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
}
